package com.anytron.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.anytron.model.AppConfigs;
import com.anytron.model.req.PageRequest;
import com.anytron.model.res.ResultData;
import com.anytron.service.AppConfigsService;
import com.anytron.service.AppInfoService;
import com.github.pagehelper.PageInfo;


@RestController
@RequestMapping(value = "app/configs")
public class AppCofigsController {
	
	@Autowired
	AppConfigsService service;
	@Autowired
	AppInfoService appInfoService;
	
	@RequestMapping(value = "list")
	public ResultData<PageInfo<AppConfigs>> list(PageRequest pageRequest,AppConfigs appConfigs) throws Exception {
		ResultData<PageInfo<AppConfigs>> resultData = new ResultData<>();
		PageInfo<AppConfigs> pageInfo = service.queryDataByPage(pageRequest, appConfigs);
		resultData.setData(pageInfo);
		resultData.setErrorCode(0);
		resultData.setSuccess(true);
		return resultData;
	}
	
	@RequestMapping(value = "add" ,method = RequestMethod.POST)
	public ResultData<String> add(@RequestBody AppConfigs appConfigs) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		AppConfigs select = new AppConfigs();
		select.setConfigName(appConfigs.getConfigName());
		select.setAppId(appConfigs.getAppId());
		AppConfigs one = service.getOne(select);
		if (one!=null) {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("当前应用存在该配置项,请重新输入");
			resultData.setSuccess(false);
			return resultData;
		}
		if (StringUtils.isBlank(appConfigs.getApolloNamespace())) {
			appConfigs.setApolloNamespace(appConfigs.getConfigName());
		}
		if (appConfigs.getNeedEffective()==null) {
			appConfigs.setNeedEffective(0);
		}
		int res = service.add(appConfigs);
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("保存失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
	
	@RequestMapping(value = "update")
	public ResultData<String> update(@RequestBody AppConfigs appConfigs) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		AppConfigs select = new AppConfigs();
		select.setConfigName(appConfigs.getConfigName());
		select.setAppId(appConfigs.getAppId());
		AppConfigs one = service.getOne(select);
		if (one!=null && one.getId().longValue() != appConfigs.getId().longValue()) {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("所选应用存在该配置项,请重新选择");
			resultData.setSuccess(false);
			return resultData;
		}
		if (StringUtils.isBlank(appConfigs.getApolloNamespace())) {
			appConfigs.setApolloNamespace(appConfigs.getConfigName());
		}
		if (appConfigs.getNeedEffective()==null) {
			appConfigs.setNeedEffective(0);
		}
		int res = service.update(appConfigs);
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("更新失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
	
	@RequestMapping(value = "updateStatus")
	public ResultData<String> updateStatus(@RequestBody AppConfigs appConfigs) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		if (appConfigs.getNeedEffective()==null) {
			appConfigs.setNeedEffective(0);
		}
		int res = service.update(appConfigs);
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("更新失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
	
	@RequestMapping(value = "delete")
	public ResultData<String> delete(@RequestBody AppConfigs appConfigs) throws Exception {
		ResultData<String> resultData = new ResultData<>();
		int res = service.delete(appConfigs.getId());
		if (res>0) {
			resultData.setErrorCode(0);
			resultData.setSuccess(true);
		}else {
			resultData.setErrorCode(1);
			resultData.setErrorMsg("删除失败");
			resultData.setSuccess(false);
		}
		return resultData;
	}
}
